package org.henzhi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.henzhi.entity.user;
import org.henzhi.service.IUserBiz;
import org.henzhi.service.imple.UserBizImple;

public class UserServlet extends HttpServlet {
   IUserBiz biz=new UserBizImple();
	
	public UserServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		//����session����
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		//��ȡurl ����Я����û���
		String flag = request.getParameter("flag");
		String uname = request.getParameter("uname");
		String upwd = request.getParameter("upwd");
		//����ҵ���߼����е�¼����
		user user = biz.login(uname);
		if("ajax".equals(flag)){
			if(user!=null){
				if(user.getRid()==1){
					out.write("true");
				}else{
					out.write("priv");//��Ȩ��
				}
			}else{//�û�������
				out.write("false");
			}
		}
		if("login".equals(flag)){//��¼����
			if(user!=null){
				if(user.getUpwd().equals(upwd)){
					session.setAttribute("user", user);
					response.sendRedirect("login.jsp");
				}else{
					response.sendRedirect("login.jsp");
				} 
			}else{
				response.sendRedirect("login.jsp");
			}
		}
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}